import re
class LocalGraph:
    def __init__(self,node1,node2,depth,fileName):
        self.Node1 = node1
        self.Node2 = node2
        self.fileName = fileName
        self.Nodes = set([node1,node2])
        self.Incoming = dict()
        self.Outgoing = dict()
        self.depth = depth
        
    def GetAllInvolvedNodes(self):
        return self.Nodes
    
    def GetBaseNodes(self):
        return (self.Node1,self.Node2)
    
    '''def GetDistance(self,node1 = None, node2 = None):
        expandNodes = set();
        expandedNodes = set();
        tempNodes = set();
        
        if(node1 == None):
            node1 = self.Node1
        if(node2 == None):
            node2 = self.Node2
        
        if(not node1 in self.Outgoing):
            return -5
        
        if(not node2 in self.Outgoing):
            return -5
        
        expandNodes.update([node1])
        d1=0
        d2=0
        found = False
        while not found:
            d1+=1
            tempNodes = set();
            if(len(expandNodes)==0):
                d1 = -1
                break
            for node in expandNodes:
                if(node == node2):
                    found = True
                    break                                                
                if not node in expandedNodes:
                    tempNodes.update(self.GetNeighbors(node,2))
                    expandedNodes.update([node])
            expandNodes = tempNodes
        
        expandNodes = set();
        expandedNodes = set();
        tempNodes = set();
        expandNodes.update([node2])
        found=False
        while not found:
            d2+=1
            tempNodes = set();
            if(len(expandNodes)==0):
                d2 = -1
                break
            for node in expandNodes:
                if(node == node2):
                    found = True
                    break                                                
                if not node in expandedNodes:
                    tempNodes.update(self.GetNeighbors(node,2))
                    expandedNodes.update([node])
            expandNodes = tempNodes
                    
        if(d1 < d2):
            return d2
        else:
            return d1'''
    def GetDistance(self,node1 = None, node2 = None):
        expandNodes = set();
        expandedNodes = set();
        tempNodes = set();
        
        if(node1 == None):
            node1 = self.Node1
        if(node2 == None):
            node2 = self.Node2
        
        if(not node1 in self.Outgoing):
            return -5
        
        if(not node2 in self.Outgoing):
            return -5
        #print "not return -5"
        expandNodes.update([node1])
        d1=0
        d2=0
        found = False
        while not found:
            d1+=1
            tempNodes = set();
            if(len(expandNodes)==0):
                d1 = -1
                break
            for node in expandNodes:
                if(node == node2):
                    found = True
                    break                                                
                if not node in expandedNodes:
                    tempNeighbor = self.GetNeighbors(node,2)
                    if  tempNeighbor == None:
                        return -1
                    
                    tempNodes.update(tempNeighbor)
                    expandedNodes.update([node])
            expandNodes = tempNodes
        
        expandNodes = set();
        expandedNodes = set();
        tempNodes = set();
        expandNodes.update([node2])
        found=False
        while not found:
            d2+=1
            tempNodes = set();
            if(len(expandNodes)==0):
                d2 = -1
                break
            for node in expandNodes:
                if(node == node2):
                    found = True
                    break                                                
                if not node in expandedNodes:
                    tempNeighbor = self.GetNeighbors(node,2)
                    if  tempNeighbor == None:
                        return -1
                    tempNodes.update(tempNeighbor)
                    expandedNodes.update([node])
            expandNodes = tempNodes
                    
        if(d1 < d2):
            return d2
        else:
            return d1
                
            
    
    def GetNeighbors(self,node,type=0):
        if node in self.Incoming and node in self.Outgoing:
            retValue = set()
            if(type==0):
                retValue.update(self.Incoming[node])
                retValue.update(self.Outgoing[node])
            elif(type==1):
                retValue.update(self.Incoming[node])
            elif(type==2):
                retValue.update(self.Outgoing[node])
            else:
                retValue.update(set(self.Incoming[node]).intersection(set(self.Outgoing[node])))
            
            return retValue
        else:
            return set()
        
